<!--宏定义 operations 参数：contextUri-->
<#macro operations contextUri >
<script type="text/javascript">
$(function(){
	<#if operationSet.hasGlobal() >
		<#list operationSet.operations('Global') as operation >
		<#if operation.bodyAfterProcess?exists && operation.bodyAfterProcess!="">
		$('#global_${action_id}').append($('<a>').click(function(){
			<#switch operation.body>
			<#case 'add' >
				eval(<@operation_create contextUri=contextUri />).call(this);
				<#break>
			<#case 'batchdelete' >
				eval(<@operation_batchdelete contextUri=contextUri />).call(this);
				<#break>
			<#case 'batchstate' >
				eval(<@operation_batchdelete contextUri=contextUri />).call(this);
				<#break>
			<#default>
				eval(${operation.bodyAfterProcess?default('')}).call(this);
				<#break>
			</#switch>
		}).linkbutton({plain:true,iconCls:'${operation.icon?default('')}',text:'${operation.title?default('')}'}));
		</#if>
		</#list>
	</#if>
});

<#if operationSet.hasContextMenu() >
var contextMenu_${action_id};
var contextMenuData_${action_id};
function	onRowContextMenu_${action_id}(e,idx,data){
	e.preventDefault();
	contextMenuData_${action_id} = data;
	if(!contextMenu_${action_id}){
		contextMenu_${action_id} = $('<div/>').appendTo('body');
		contextMenu_${action_id}.menu();
		
		<#list operationSet.operations('ContextMenu') as operation >
			<@appendMenu operation=operation pid="" contextUri=contextUri />
		</#list>
	}
	
	<#list operationSet.operations('ContextMenu') as operation >
		<@checkMenuEnabled operation=operation />
	</#list>
	
	contextMenu_${action_id}.menu('show',{
		left: e.pageX,
		top: e.pageY
	});
}
</#if>


</script>
</#macro>

<#macro appendMenu operation pid contextUri >
	contextMenu_${action_id}.menu('appendItem', {
		<#if pid?has_content>parent:contextMenu_${action_id}.menu('findItem','${pid}').target,</#if>
		text: '${operation.title?default('')}',
		iconCls: '${operation.icon?default('')}',
		id:'${operation.id?default('')}',
		<#if operation.bodyAfterProcess?exists && operation.bodyAfterProcess!="">
		onclick:function(item){
			<#switch operation.body>
			<#case 'update' >
				eval(<@operation_update contextUri=contextUri />).call(this,contextMenuData_${action_id}.id);
				<#break>
			<#case 'delete' >
				eval(<@operation_delete contextUri=contextUri />).call(this,contextMenuData_${action_id}.id);
				<#break>
			<#case 'state_no' >
				eval(<@operation_state contextUri=contextUri />).call(this,contextMenuData_${action_id}.id,-1);
				<#break>
			<#case 'state_yes' >
				eval(<@operation_state contextUri=contextUri />).call(this,contextMenuData_${action_id}.id,1);
				<#break>
			<#case 'state_finish' >
				eval(<@operation_state contextUri=contextUri />).call(this,contextMenuData_${action_id}.id,9);
				<#break>
			<#default>
				eval(${operation.bodyAfterProcess?default('')}).call(this,contextMenuData_${action_id});
				<#break>
			</#switch>
		}
		</#if>		
	});
	
	<#if operation.menu>
		<#list operation.children as chop >
			<@appendMenu operation=chop pid=operation.title contextUri=contextUri  />
		</#list>
	</#if>
</#macro>

<#macro checkMenuEnabled operation >
	<#if operation.checkAfterProcess?exists && operation.checkAfterProcess!="">
		if(!eval(${operation.checkAfterProcess?default('')}).call(this,contextMenuData_${action_id})){
			contextMenu_${action_id}.menu('disableItem',contextMenu_${action_id}.find('#${operation.id}'));
		}else{
			contextMenu_${action_id}.menu('enableItem',contextMenu_${action_id}.find('#${operation.id}'));
		}
	</#if>
	<#list operation.children as chop >
		<@checkMenuEnabled operation=chop />
	</#list>
</#macro>







<#macro operation_create contextUri >
function add_${action_id}(){
	$('<div/>').dialog({
		href:'${request.contextPath}${namespace?default('')}/${contextUri}-form.htm?action_id=${action_id}',
		width:600,   
		height:500,   
		title: '${action.getText('bg.datagrid.add')}',
		modal:true,
		maximizable:true,
		collapsible:false,
		minimizable:false,
		resizable:true,
		buttons:[{iconCls : 'icon-ok',
					text:'${action.getText('bg.datagrid.save')}',
					handler:function(){
						var d = $(this).closest('.window-body');
						$('#form_${action_id}').form('submit', {
							url : '${request.contextPath}${namespace?default('')}/${contextUri}-add.json',
							onSubmit: function(){
								return $(this).form('validate');
	        				},
							success : function(json) {
								eval("json="+json);
								$.messager.alert(json.title,json.msg,json.level);
								d.dialog('destroy');
								$("#datagrid_${action_id}").datagrid('reload');
							}
						});
					}}],
		onClose : function() {
			if(typeof dialogClose_${action_id} != 'undefined'){
				dialogClose_${action_id}();
			}
			$(this).dialog('destroy');
		}
	});
}
</#macro>

<#macro operation_update contextUri >
function edit_${action_id}(id){
	$('<div>').dialog({
		href : '${request.contextPath}${namespace?default('')}/${contextUri}-edit.htm?ajax=false&action_id=${action_id}&id='+id,
		width:600,   
		height:500,   
		modal : true,
		maximizable:true,
		collapsible:false,
		minimizable:false,
		resizable:true,
		title: '${action.getText('bg.datagrid.edit')}',
		buttons : [ {
			text : '${action.getText('bg.datagrid.edit')}',
			iconCls : 'icon-edit',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#form_${action_id}').form('submit', {
					url : '${request.contextPath}${namespace?default('')}/${contextUri}-update.json',
					onSubmit: function(){
						return $(this).form('validate');
    				},
					success : function(json) {
						eval("json="+json);
						$.messager.alert(json.title,json.msg,json.level);
						d.dialog('destroy');
						$("#datagrid_${action_id}").datagrid('reload');
					}
				});
			}
		} ],
		onClose : function() {
			if(typeof dialogClose_${action_id} != 'undefined'){
				dialogClose_${action_id}();
			}
			$(this).dialog('destroy');
		}
	});
}
</#macro>

<#macro operation_delete contextUri >
function delete_${action_id}(id){
	$.messager.confirm('${action.getText('bg.message.info')}', '${action.getText('bg.datagrid.delete.ask')}', function(b) {
		if (b) {
			$.ajax({
				url : '${request.contextPath}${namespace?default('')}/${contextUri}-delete.json',
				data : {
					id : id
				},
				dataType : 'JSON',
				success : function(json) {
					$.messager.alert(json.title,json.msg,json.level);
					if(json.level!='error'){
						$("#datagrid_${action_id}").datagrid('reload');
					}
				}
			});
		}
	});
}
</#macro>

<#macro operation_batchdelete contextUri >
function batchdelete_${action_id}(){
	var ids = [];
	var rows = $('#datagrid_${action_id}').datagrid('getSelections');
	if(rows.length==0){
		$.messager.alert('${action.getText('system.message.alert')}', '${action.getText('system.delete.select')}', 'warning');
	}
	
	for(var i=0;i<rows.length;i++){
		ids.push(rows[i].id);
	}
	if(ids.length>0){
		$.messager.confirm('${action.getText('bg.message.info')}', '${action.getText('bg.datagrid.delete.ask')}', function(b) {
			if (b) {
				$.ajax({
					url : '${request.contextPath}${namespace?default('')}/${contextUri}-delete.json',
					data:{deletes : ids.join(';')},
					dataType : 'JSON',
					success : function(json) {
						$.messager.alert(json.title,json.msg,json.level);
						if(json.level!='error'){
							$("#datagrid_${action_id}").datagrid('reload');
						}
					}
				});
			}
		});
	}
}
</#macro>

<#macro operation_state contextUri >
function state_${action_id}(id,state){
	$.ajax({
		url : '${request.contextPath}${namespace?default('')}/${contextUri}-state.json',
		data:{'id':id,'state':state},
		dataType : 'JSON',
		success : function(json) {
			//eval("json="+json);
			$.messager.alert(json.title,json.msg,json.level);
			$("#datagrid_${action_id}").datagrid('reload');
		}
	});
}
</#macro>

<#macro operation_batchstate contextUri >
function state_${action_id}(state){
	var ids = [];
	var rows = $('#datagrid_${action_id}').datagrid('getSelections');
	if(rows.length==0){
		$.messager.alert('${action.getText('system.message.alert')}', '${action.getText('system.delete.select')}', 'warning');
	}
	
	for(var i=0;i<rows.length;i++){
		ids.push(rows[i].id);
	}
	if(ids.length>0){
		$.messager.confirm('${action.getText('bg.message.info')}', '${action.getText('bg.datagrid.delete.ask')}', function(b) {
			if (b) {
				$.ajax({
					url : '${request.contextPath}${namespace?default('')}/${contextUri}-state.json',
					data:{deletes : ids.join(';')},
					dataType : 'JSON',
					success : function(json) {
						$.messager.alert(json.title,json.msg,json.level);
						if(json.level!='error'){
							$("#datagrid_${action_id}").datagrid('reload');
						}
					}
				});
			}
		});
	}
}
</#macro>